package com.kaifamiao.schedule;

/**
 * 1、优先级越高的线程越优先获得CPU时间片
 * 2、Thread类中有三个表示优先级的常量
 *      Thread.MIN_PRIORITY
 *      Thread.NORM_PRIORITY
 *      Thread.MAX_PRIORITY
 * 3、Thread # setPriority( int )
 * 4、Thread # getPriority()
 */
public class PriorityTest {

    static class PriorityThread extends Thread {

        public PriorityThread(String name, int priority) {
            super(name); // 调用父类构造并传递参数
            this.setPriority(priority); // 设置线程优先级
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            for (int i = 1; i < 6; i++) {
                System.out.printf( "%s - %d\n", name, i );
            }
        }

    }

    public static void main(String[] args) {

        Thread t = new PriorityThread( "孙悟空" , Thread.MIN_PRIORITY );
        t.start();

        Thread h = new PriorityThread( "猪悟能" , Thread.MIN_PRIORITY + 2 );
        h.start();

        Thread x = new PriorityThread( "沙悟净" , Thread.MAX_PRIORITY );
        x.start();

    }

}
